﻿using HandyControl.Controls;
using MyHelper;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;

namespace weighing
{
    public partial class AboutWindow
    {
        public AboutWindow()
        {
            InitializeComponent();

            CommonFunction.ShowCustomIcon(this);

            ShowCustomIcon();

            DataContext = this;
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {            
            CopyRight = App.Config.SystemConfig.CopyRight;      
            Version = $"v 1.0.0";                  
            this.SystemName.Text = "称重管理系统";
            this.clientIdTb.Text = App.InitConfig.clientId.ToString();
            this.dbTypeTb.Text = "储存类型:Sqlite 3 ";
            this.versionNameTb.Text = "单机版本";   
            
            if(App.CurrentUser?.phone == "18087467482")
            {
                this.ActiveInfoBtn.Visibility = Visibility.Visible;
            }
            else
            {
                this.ActiveInfoBtn.Visibility = Visibility.Collapsed;
            }
        }

        private void ShowCustomIcon()
        {
            string iconfile = Constant.CustomIconFile;
            if (File.Exists(iconfile))
            {
                IconImg.Source = BitmapHelper.FromString(iconfile);
            }
        }
        private void Window_ContentRendered(object sender, EventArgs e)
        {
            if (App.initConfig.ActiveType == ActiveType.Code)
            {
                if (ActiveCodeHelper.IsSoftActive())
                {
                    string t = (ActiveCodeHelper.IsGernalCode(App.Config.ActiveCode) ? "通用码激活 " : "已激活 ");
                    if (ActiveCodeHelper.IsNeverCode(App.Config.ActiveCode))
                    {
                        t += "永久使用";
                    }
                    else
                    {
                        t += ActiveCodeHelper.GetEffictiveTime(App.Config.ActiveCode);
                    }
                    this.ActiveInfoTb.Text = t;
                }
                var str = "使用公司：" + App.CurrentCompanyVo?.FullName + " ID:" + App.CurrentCompanyVo?.Id;
                this.useCompanyTb.Text = str;
            }
            else
            {
                this.ActiveInfoTb.Text = "软件未读取加密狗";
                if (ActiveDongleHelper.IsActived())
                {
                    this.ActiveInfoTb.Text = string.Empty;
                }
                var str = "使用公司：" + App.CurrentCompanyVo?.FullName + " ID:" + App.CurrentCompanyVo?.Id;
                this.useCompanyTb.Text = str;
            }

        }

        public static readonly DependencyProperty CopyRightProperty = DependencyProperty.Register(
            "CopyRight", typeof(string), typeof(AboutWindow), new PropertyMetadata(default(string)));

        public string CopyRight
        {
            get => (string)GetValue(CopyRightProperty);
            set => SetValue(CopyRightProperty, value);
        }

        public static readonly DependencyProperty VersionProperty = DependencyProperty.Register(
            "Version", typeof(string), typeof(AboutWindow), new PropertyMetadata(default(string)));

        public string Version
        {
            get => (string)GetValue(VersionProperty);
            set => SetValue(VersionProperty, value);
        }

        private void Window_KeyUp(object sender, System.Windows.Input.KeyEventArgs e)
        {
            if (e.Key == Key.Escape)
            {
                this.Close();
            }
        }

        private void Hyperlink_Click(object sender, RoutedEventArgs e)
        {
            new ActiveWindow() { Owner = App.Current.MainWindow }.Show();
            this.Close();
        }

        //激活码
        private void CodeBtn_Click(object sender, RoutedEventArgs e)
        {
            new TestActiveWindow() { }.ShowDialog();
        }
    }
}
